package common

import (
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	commonDto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/common"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/common"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type DmpReportController struct {
	controller.BaseController
	dmpReportService common.DmpReportServiceIface
}

func NewDmpReportController(dmpReportService common.DmpReportServiceIface) *DmpReportController {
	return &DmpReportController{
		dmpReportService: dmpReportService,
	}
}

func (d *DmpReportController) RegisterRouter(g *gin.RouterGroup) {
	g.GET("/dmp-report/get-sso-url", d.GetSsoLoginUrl)
}

func (d *DmpReportController) GetSsoLoginUrl(ctx *gin.Context) {
	// 1. 校验参数
	request := &commonDto.DmpLoginUrlRequest{}
	if err := ctx.ShouldBindQuery(&request); err != nil {
		d.Fail(ctx, errcode.COMMON_PARAMS_ERROR, err.Error())
		return
	}

	// 2. 调用方法获取
	resp, err := d.dmpReportService.GetSsoLoginUrl(ctx, request)
	if err != nil {
		stark.Logger.Info(ctx, "get-dmp-sso-login-failed: "+err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		d.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}

	d.Success(ctx, resp)
}
